//
//  CQVistorView.swift
//  swift-weibo
//
//  Created by 陈乾 on 15/10/26.
//  Copyright © 2015年 陈乾. All rights reserved.
//

import UIKit

protocol CQVistorViewDelegate: NSObjectProtocol {
 
    func vistorViewRegisterClick()
    func vistorViewLoginClick()
}

class CQVistorView: UIView {
    
    //代理属性 一定要用weak修饰 可能为空 所以要用可选类型修饰
    weak var vistorDelegate: CQVistorViewDelegate?
     //MARK: -  登陆 注册按钮点击函数
    func registerClick() {
     //调用注册代理方法
     vistorDelegate?.vistorViewRegisterClick()
    }
    
    func loginClick() {
        //调用登陆代理方法
        vistorDelegate?.vistorViewLoginClick()
    }
    
    //MARK: - 程序进口
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    //MARK: - 创建定时器
    func createAnimation() {
     
       let annimation = CABasicAnimation(keyPath: "transform.rotation")
        annimation.toValue = 2*M_PI
        annimation.repeatCount = MAXFLOAT
        annimation.duration = 20
        //加上这一句不然 切换控制器会停止
        annimation.removedOnCompletion = false
        //添加动画到图层
        turntableView.layer.addAnimation(annimation, forKey: "xxoo")

       
        
     
    }
    
    //MARK: - 设置除首页之外的其他控制器的图
    func setUpCoverView(imageName: String, message: String) {
      
        //隐藏房子
        houseView.hidden = true;
        //设置
        turntableView.image = UIImage(named: imageName)
        titleLabel.text = message
        
        //把朦胧的view带到后面去
        self.sendSubviewToBack(coverView)
    
    }
    
    //MARK: - 准备UI
    private func prepareUI() {
        
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        //添加子控件
        addSubview(turntableView)
        //必须把朦胧view添加在这中间
        addSubview(coverView)
        addSubview(houseView)
        addSubview(titleLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //把约束干扰去掉
        turntableView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //设置约束
        /**
        *轮盘图片约束
        */
        
        addConstraint(NSLayoutConstraint(item: turntableView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: turntableView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        /**
        *  房子的约束
        */
        addConstraint(NSLayoutConstraint(item: houseView , attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: turntableView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView , attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: turntableView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        /**
        *  设置label的约束
        */
        //居中
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: turntableView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //距离轮盘图片底部16
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: turntableView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //labe的宽度
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        //labe的高度
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 44))
        
        /**
        *  设置按钮的约束
        */
        //与label左边对器
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: titleLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        //与label底部距离16
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽、高
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 38))
        
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: titleLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        //与label底部距离16
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽、高
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 38))
        
        
        //设置朦胧View的约束
        //左边和父亲view对齐
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        
        
    }
    
    //MARK: - 懒加载
    ///轮盘图片
    private lazy var turntableView: UIImageView = {
        //创建
        let turntable = UIImageView()
        //设置图片
        turntable.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        //设置size
        turntable.sizeToFit()
        
        return turntable
        }()
    ///房子图片
    private lazy var houseView: UIImageView = {
        //创建
        let h = UIImageView()
        //设置图片
        h.image = UIImage(named: "visitordiscover_feed_image_house")
        //设置size
        h.sizeToFit()
        //返回
        return h
        }()
    /// 文字label
    private lazy var titleLabel: UILabel = {
        //创建
        let label = UILabel()
        //设置文字
        label.text = "关注一些人,看看有什么惊喜"
        //颜色
        label.textColor = UIColor.orangeColor()
        //设置size
        label.sizeToFit()
        //设置换行
        label.numberOfLines = 0
        return label
        
        }()
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        //创建
        let button = UIButton()
        //设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //监听按钮点击
        button.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        //设置文字
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.purpleColor(), forState: UIControlState.Normal)
        //设置size
        button.sizeToFit()
        
        return button
        }()
    
    /// 创建登陆按钮
    private lazy var loginButton: UIButton = {
        //创建
        let button = UIButton()
        //设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //监听按钮点击
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)

        //设置文字
        button.setTitle("登陆", forState: UIControlState.Normal)
        //设置颜色
        button.setTitleColor(UIColor.purpleColor(), forState: UIControlState.Normal)
        //设置size
        button.sizeToFit()
        
        return button
        }()
    
    //懒加载朦胧view
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}
